package xyz.weizhi.rjwm.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.weizhi.rjwm.entity.*;
import xyz.weizhi.rjwm.entity.vo.DishDto;
import xyz.weizhi.rjwm.entity.vo.SetmealDto;
import xyz.weizhi.rjwm.mapper.DishDao;
import xyz.weizhi.rjwm.service.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author milian
 * @Date 2021/9/25 0025 18:43
 */
@Service
public class DishServiceImp extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @Transactional
    @Override
    public boolean saveDishAndDishFlavor(DishDto dishDto) {
        //将菜品信息保存到Dish表中
        boolean save = this.save(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        boolean b = dishFlavorService.saveBatch(flavors);
        return save && b;
    }

    @Override
    public DishDto getDishAndDishFlavorById(long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Transactional
    @Override
    public boolean updateDishAndDishFlavor(DishDto dishDto) {
        boolean b = this.updateById(dishDto);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map(item -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        boolean b1 = dishFlavorService.updateBatchById(flavors);
        return b && b1;
    }

    @Transactional
    @Override
    public boolean deleteDishAndDishFlavor(long ids[]) {
        try {
            for (long id : ids) {
                this.removeById(id);
                dishFlavorService.removeById(id);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Transactional
    @Override
    public boolean updatDishStatusByStatus(long[] ids, Integer status) {
        try {
            for (long id : ids) {
                LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Dish::getId, id);
                Dish dish = this.getOne(lqw);
                dish.setStatus(status);
                this.update(dish, lqw);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
